package cn.csust.net2.manager.client.chat;


import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.client.ux.BeanTreeFactory;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.po.User;
import cn.csust.net2.manager.shared.service.BeanService;
import cn.csust.net2.manager.shared.service.BeanServiceAsync;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;


@SuppressWarnings("deprecation")
public class UserTreePanel extends LayoutContainer {
	
	BeanServiceAsync<Student> studentService = GWT.create(BeanService.class);
	
	ContentPanel westPanel = new ContentPanel();
	ContentPanel centerPanel = new ContentPanel();
	ContentPanel eastPanel = new ContentPanel();
	
	BorderLayoutData westData;
	BorderLayoutData centerData;
	BorderLayoutData eastData;
	 
	TreePanel<BeanModel> treePanel;
	public static TabPanel chatTabPanel  = new TabPanel(){
		
		//重写关闭tabitem方法
		@Override
		public void close(TabItem item){
			chatTabPanel.remove(item);
			ChatTab ct = (ChatTab)item;
			App.getChatPool().disposeChatTab(ct.getReceiver().getUserID());
		}
	};
	
	private User user;

	public UserTreePanel(User user) {
		this.setUser(user);
		init();
	}

	
	void initBorderLayoutData(){
		
		
		westData = new BorderLayoutData(LayoutRegion.WEST, 200);   
	    westData.setSplit(true);   
	    westData.setCollapsible(true);   
	    westData.setMargins(new Margins(0,5,0,0));   
	  
	    centerData = new BorderLayoutData(LayoutRegion.CENTER);   
	    centerData.setMargins(new Margins(0));   
	  
	    eastData = new BorderLayoutData(LayoutRegion.EAST, 200);   
	    eastData.setSplit(true);   
	    eastData.setCollapsible(true);   
	    eastData.setMargins(new Margins(0,0,0,5));   

		
	}
	protected void init() {
		
		this.setLayout(new BorderLayout()); 
		initBorderLayoutData();
		
		treePanel = BeanTreeFactory.createComonTree();
		/*
		 * 在TreePanel上监听双击事件，为：Events.OnDoubleClick, 而不是DoubleClick 
		 * 同样Grid上的双击也是OnDoubleClick 
		 */
		treePanel.addListener(Events.OnDoubleClick, new Listener<TreePanelEvent<BeanModel>>(){

			@Override
			public void handleEvent(TreePanelEvent<BeanModel> be) {
				// TODO Auto-generated method stub
				BeanModel bm = be.getItem();
				
				//判断是否为用户节点
				if(!bm.get("userID").equals(null)){
					Student student = new Student();
					student.setUserID(new Long(bm.get("userID").toString()));
					student.setName(bm.get("name").toString());
					
					studentService.findByUniqueExample("Student", student, new AsyncCallback<PO>() {
						
						@Override
						public void onSuccess(PO result) {
							// TODO Auto-generated method stub
							App.getChatPool().showChatTab((Student)result);
						}
						
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}
					});
				}
				
			}
			 
		});
		westPanel.setHeading("用户");
		westPanel.setLayout(new FitLayout());
		westPanel.add(treePanel);
		
		chatTabPanel.setResizeTabs(true); 
		chatTabPanel.setMinTabWidth(60);
		chatTabPanel.setResizeTabs(true);   
		chatTabPanel.setAnimScroll(true);   
		chatTabPanel.setTabScroll(true); 
	    chatTabPanel.setCloseContextMenu(true); 

		centerPanel.setHeading("聊天");
		centerPanel.setLayout(new FitLayout());
		centerPanel.add(chatTabPanel);
		
		eastPanel.setHeading("待定");
		
		this.add(westPanel, westData);
		this.add(centerPanel, centerData);
		this.add(eastPanel, eastData);
		
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public static TabPanel getChatTabPanel() {
		return chatTabPanel;
	}


	public static void setChatTabPanel(TabPanel chatTabPanel) {
		UserTreePanel.chatTabPanel = chatTabPanel;
	}
	
	

}
